
package ext.acae2.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * 
 * @desription 用于处理类型ini格式文件的辅助程序，数据将缓存在内存中，因此只适用于小型的配置文件
 * @author cnie
 * @date 2013-4-11 下午7:07:57
 */
@SuppressWarnings("unchecked")
public class CmIniFileUtil
{
   private static final Set TRUES           = new HashSet<String>(Arrays.asList(new String[]{"TRUE", "YES", "ON", "1"}));

   private Map              contents        = new HashMap();
   private Map              sectionContents = Collections.EMPTY_MAP;

   public CmIniFileUtil(String filename) {
      try
      {
         loadContents(new FileInputStream(filename));
      } catch (Exception e)
      {
         e.printStackTrace();
      }
   }

   public CmIniFileUtil(InputStream is) {
      try
      {
         loadContents(is);
      } catch (Exception e)
      {
         e.printStackTrace();
      }
   }

   public CmIniFileUtil(Map sectionContents) {
      if (sectionContents == null)
         sectionContents = Collections.EMPTY_MAP;
      this.sectionContents = sectionContents;
   }

   public Map sectionContents()
   {
      return sectionContents;
   }

   public CmIniFileUtil getSection(String section)
   {
      sectionContents = (Map) contents.get(section.trim());
      if (sectionContents == null)
         sectionContents = Collections.EMPTY_MAP;
      return this;
   }

   public Set keySet()
   {
      return sectionContents.keySet();
   }

   public String get(String key)
   {
      return (String) sectionContents.get(key);
   }

   public String get(String key, String defaultVal)
   {
      String val = (String) sectionContents.get(key);
      return val == null ? defaultVal : val;
   }

   public int get(String key, int defaultVal)
   {
      String val = (String) sectionContents.get(key);
      if (val == null)
         return defaultVal;

      return Integer.parseInt(val);
   }

   public float get(String key, float defaultVal)
   {
      String val = (String) sectionContents.get(key);
      if (val == null)
         return defaultVal;

      return Float.parseFloat(val);
   }

   public double get(String key, double defaultVal)
   {
      String val = (String) sectionContents.get(key);
      if (val == null)
         return defaultVal;

      return Double.parseDouble(val);
   }

   public boolean get(String key, boolean defaultVal)
   {
      String val = (String) sectionContents.get(key);
      if (val == null)
         return defaultVal;

      return TRUES.contains(val.toUpperCase()) ? true : false;
   }

   public void put(String key, String value)
   {
      if (key != null)
      {
         if (Collections.EMPTY_MAP.equals(sectionContents))
            sectionContents = new HashMap<String, String>();
         sectionContents.put(key, value);
      }
   }

   public void put(String key, int value)
   {
      put(key, String.valueOf(value));
   }

   public void put(String key, double value)
   {
      put(key, String.valueOf(value));
   }

   public void put(String key, boolean value)
   {
      put(key, String.valueOf(value));
   }

   private void loadContents(InputStream is) throws Exception
   {
      BufferedReader r = new BufferedReader(new InputStreamReader(is, "GB18030"));

      String line;
      String section = null;
      Map sectMap = null;
      while ((line = r.readLine()) != null)
      {
         if (line.startsWith("#") || line.startsWith(";"))
            continue;

         if (line.startsWith("[") && line.indexOf("]") > 0)
         {
            section = line;
            sectMap = new HashMap();
            contents.put(section, sectMap);
            contents.put(section.substring(1, line.length() - 1).trim(), sectMap);
            continue;
         }

         if (section == null)
            continue;

         int deliPos = line.indexOf("=");
         if (deliPos < 0)
            continue;
         String key = line.substring(0, deliPos).trim();
         String val = line.substring(deliPos + 1, line.length()).trim();
         if (key.startsWith("\"") && key.endsWith("\""))
            key = key.substring(1, key.length() - 1);
         if (val.startsWith("\"") && val.endsWith("\""))
            val = val.substring(1, val.length() - 1);
         sectMap.put(key, val);
      }
   }
}
